//
//  BedsListTableViewController.m
//  PlotMate
//
//  Created by Brian Boyle on 05/04/2012.
//  Copyright (c) 2012 Brian Boyle. All rights reserved.
//

#import "BedsListTableViewController.h"
#import "AppDelegate.h"
#import "Bed.h"
#import "BedDetailTableViewController.h"
#import "UITableView+TableSectionHeaderText.h"
#import "UILabel+TextFont.h"

@interface BedsListTableViewController()
@property (nonatomic, strong) NSManagedObjectContext *context;
@end


@implementation BedsListTableViewController
@synthesize context = _context;
@synthesize plot = _plot;

/*Set the NSFetchedResultsController with the results of loading all the beds for a specific plot*/
- (void)setupFetchedResultsController {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Bed"];
    request.predicate = [NSPredicate predicateWithFormat:@"plot.name = %@", self.plot.name];
    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sorter];
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil];
}

#pragma mark - View lifecycle

-(void)viewDidLoad
{
    //Get the managedObjectContext from the AppDelegate
    self.context = [(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];
    
    //Set the background image
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plot_bg.png"]];
    [tempImageView setFrame:self.tableView.frame]; 
    self.tableView.backgroundView = tempImageView;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = self.plot.name;
    [self setupFetchedResultsController];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table View Delegates

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"textWithBugCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    Bed *bed = [self.fetchedResultsController objectAtIndexPath:indexPath];
        
    UILabel *label = (UILabel *)[cell viewWithTag:1];
    label.text = bed.name;
    [label setSketchBlockFont];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return @"Your Beds";
}

/*Custom label in the section header*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSString *sectionTitle = [self tableView:self.tableView titleForHeaderInSection:section]; 
    return [UITableView createSectionHeaderTextLabel:sectionTitle];
}

    
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Bed *selectedBed = [self.fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"ViewBedDetail" sender:selectedBed];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"ViewBedDetail"]){
        [segue.destinationViewController setBed:sender];
    }
}

@end
